package com.netty.tcp;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NettyTcpApp {
    public static void main(String[] args) {
        start(1024);

    }

    private static void start(int port) {
        long starttime = System.currentTimeMillis();
        ServerBootstrap server = new ServerBootstrap();
        server.group(new NioEventLoopGroup(), new NioEventLoopGroup());

        try {
            server  .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG,128)
                    .childOption(ChannelOption.SO_KEEPALIVE,true)

                    .childHandler(new ServerChannelInitializer());

            ChannelFuture fu = server.bind(port).sync();
            long startedtime = System.currentTimeMillis();
            log.info("服务器启动成功，端口：" + port + " 耗时：" + (startedtime - starttime));
            fu.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
